package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.truck;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CTrucks;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICTrucksService;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class TagBu {
    @Autowired
    private ICTrucksService icTrucksService;
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private UserInfoDao userInfoDao;

/*    //发放标签
    public SzResult addTag(String id, String tag, String token) throws Exception {
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks cTrucks = icTrucksService.getOne(queryWrapper);
        if (cTrucks.getTrucksTag1() != null && !cTrucks.getTrucksTag1().equals("")) {
            if (cTrucks.getTrucksTag2() != null && !cTrucks.getTrucksTag2().equals("")) {
                if (cTrucks.getTrucksTag3() != null && !cTrucks.getTrucksTag3().equals("")) {
                    return SzResult.error("标签已满");
                } else {
                    cTrucks.setTrucksTag3(tag);
                }
            } else {
                cTrucks.setTrucksTag2(tag);
            }
        } else {
            cTrucks.setTrucksTag1(tag);
        }
        icTrucksService.updateById(cTrucks);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String trucksTruckno = cTrucks.getTrucksTruckno();
        sysLogBu.logRecord("发放标签", user.getTrueName(), "发放了车牌号为" + trucksTruckno + "的标签");
        return SzResult.success("标签发放成功");
    }*/
 /*   //更换标签1
    public SzResult updateTag1(String id, String tag, String token) throws Exception {
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks cTrucks = icTrucksService.getOne(queryWrapper);
        //判断标签1是否存在，存在则更换，不存在则报错
        if (cTrucks.getTrucksTag1() != null && !cTrucks.getTrucksTag1().equals("")) {
            cTrucks.setTrucksTag1(tag);
        } else {
            return SzResult.error("标签1不存在");
        }
        icTrucksService.updateById(cTrucks);
        //记录日志
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String trucksTruckno = cTrucks.getTrucksTruckno();
        sysLogBu.logRecord("更换标签", user.getTrueName(), "更换了车牌号为" + trucksTruckno + "的标签");
        return SzResult.success("标签更换成功");
    }

    //更换标签2
    public SzResult updateTag2(String id, String tag, String token) throws Exception {
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks cTrucks = icTrucksService.getOne(queryWrapper);
        //判断标签2是否存在，存在则更换，不存在则报错
        if (cTrucks.getTrucksTag2() != null && !cTrucks.getTrucksTag2().equals("")) {
            cTrucks.setTrucksTag2(tag);
        } else {
            return SzResult.error("标签2不存在");
        }
        icTrucksService.updateById(cTrucks);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String trucksTruckno = cTrucks.getTrucksTruckno();
        sysLogBu.logRecord("更换标签", user.getTrueName(), "更换了车牌号为" + trucksTruckno + "的标签");
        return SzResult.success("标签更换成功");
    }

    //更换标签3
    public SzResult updateTag3(String id, String tag, String token) throws Exception {
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks cTrucks = icTrucksService.getOne(queryWrapper);
        //判断标签3是否存在，存在则更换，不存在则报错
        if (cTrucks.getTrucksTag3() != null && !cTrucks.getTrucksTag3().equals("")) {
            cTrucks.setTrucksTag3(tag);
        } else {
            return SzResult.error("标签3不存在");
        }
        icTrucksService.updateById(cTrucks);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String trucksTruckno = cTrucks.getTrucksTruckno();
        sysLogBu.logRecord("更换标签", user.getTrueName(), "更换了车牌号为" + trucksTruckno + "的标签");
        return SzResult.success("标签更换成功");
    }*/
/*    //发放标签1
    public SzResult addTag1(String id, String tag, String token) throws Exception {
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks cTrucks = icTrucksService.getOne(queryWrapper);
        //判断标签1是否存在，存在则报错，不存在则发放
        if (cTrucks.getTrucksTag1() != null && !cTrucks.getTrucksTag1().equals("")||cTrucks.getTrucksTag2() != null && !cTrucks.getTrucksTag2().
                equals("")||cTrucks.getTrucksTag3() != null && !cTrucks.getTrucksTag3().equals("")) {
            return SzResult.error("该车辆已有该标签,请勿重复发放");
        } else {
            cTrucks.setTrucksTag1(tag);
        }
        icTrucksService.updateById(cTrucks);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String trucksTruckno = cTrucks.getTrucksTruckno();
        sysLogBu.logRecord("发放标签", user.getTrueName(), "发放了车牌号为" + trucksTruckno + "的标签");
        return SzResult.success("标签发放成功");
    }

    //发放标签2
    public SzResult addTag2(String id, String tag, String token) throws Exception {
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks cTrucks = icTrucksService.getOne(queryWrapper);
        //判断标签2是否存在，存在则报错，不存在则发放
        if (cTrucks.getTrucksTag2() != null && !cTrucks.getTrucksTag2().equals("")||cTrucks.getTrucksTag1() != null && !cTrucks.getTrucksTag1().
                equals("")||cTrucks.getTrucksTag3() != null && !cTrucks.getTrucksTag3().equals("")) {
            return SzResult.error("该车辆已有该标签,请勿重复发放");
        } else {
            cTrucks.setTrucksTag2(tag);
        }
        icTrucksService.updateById(cTrucks);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String trucksTruckno = cTrucks.getTrucksTruckno();
        sysLogBu.logRecord("发放标签", user.getTrueName(), "发放了车牌号为" + trucksTruckno + "的标签");
        return SzResult.success("标签发放成功");
    }

    //发放标签3
    public SzResult addTag3(String id, String tag, String token) throws Exception {
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks cTrucks = icTrucksService.getOne(queryWrapper);
        //判断标签3是否存在，存在则报错，不存在则发放
        if ((cTrucks.getTrucksTag3() != null && !cTrucks.getTrucksTag3().equals(""))||(cTrucks.getTrucksTag1() != null && !cTrucks.getTrucksTag1().
                equals(""))||(cTrucks.getTrucksTag2() != null && !cTrucks.getTrucksTag2().equals(""))) {
            return SzResult.error("该车辆已有该标签,请勿重复发放");
        } else {
            cTrucks.setTrucksTag3(tag);
        }
        icTrucksService.updateById(cTrucks);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String trucksTruckno = cTrucks.getTrucksTruckno();
        sysLogBu.logRecord("发放标签", user.getTrueName(), "发放了车牌号为" + trucksTruckno + "的标签");
        return SzResult.success("标签发放成功");
    }

    //删除标签1
    public SzResult deleteTag1(String id, String token) throws Exception {
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks cTrucks = icTrucksService.getOne(queryWrapper);
        //判断标签1是否存在，存在则删除，不存在则报错
        if (cTrucks.getTrucksTag1() != null && !cTrucks.getTrucksTag1().equals("")) {
            cTrucks.setTrucksTag1("");
        } else {
            return SzResult.error("标签1不存在");
        }
        icTrucksService.updateById(cTrucks);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String trucksTruckno = cTrucks.getTrucksTruckno();
        sysLogBu.logRecord("删除标签", user.getTrueName(), "删除了车牌号为" + trucksTruckno + "的标签");
        return SzResult.success("标签删除成功");
    }

    //删除标签2
    public SzResult deleteTag2(String id, String token) throws Exception {
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks cTrucks = icTrucksService.getOne(queryWrapper);
        //判断标签2是否存在，存在则删除，不存在则报错
        if (cTrucks.getTrucksTag2() != null && !cTrucks.getTrucksTag2().equals("")) {
            cTrucks.setTrucksTag2("");
        } else {
            return SzResult.error("标签2不存在");
        }
        icTrucksService.updateById(cTrucks);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String trucksTruckno = cTrucks.getTrucksTruckno();
        sysLogBu.logRecord("删除标签", user.getTrueName(), "删除了车牌号为" + trucksTruckno + "的标签");
        return SzResult.success("标签删除成功");
    }

    //删除标签3
    public SzResult deleteTag3(String id, String token) throws Exception {
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks cTrucks = icTrucksService.getOne(queryWrapper);
        //判断标签3是否存在，存在则删除，不存在则报错
        if (cTrucks.getTrucksTag3() != null && !cTrucks.getTrucksTag3().equals("")) {
            cTrucks.setTrucksTag3("");
        } else {
            return SzResult.error("标签3不存在");
        }
        icTrucksService.updateById(cTrucks);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String trucksTruckno = cTrucks.getTrucksTruckno();
        sysLogBu.logRecord("删除标签", user.getTrueName(), "删除了车牌号为" + trucksTruckno + "的标签");
        return SzResult.success("标签删除成功");
    }*/
    //发放标签
    public SzResult addTag(String id, String tag, Integer value, String token,String plantId) throws Exception {
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        CTrucks cTrucks = icTrucksService.getOne(queryWrapper);
        if (cTrucks == null) {
            return SzResult.Error("该车辆不存在");
        }
//        如果标签发给tag或者tag2或者tag3，某个车子了
        QueryWrapper<CTrucks> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("TRUCKS_DELETED", 0);
        queryWrapper1.eq("DATA_FROM", plantId);
        queryWrapper1.and(i ->i.eq("TRUCKS_TAG1",tag).or().eq("TRUCKS_TAG2",tag).or().eq("TRUCKS_TAG3",tag) );
        List<CTrucks> one = icTrucksService.list(queryWrapper1);
        if (one != null && one.size()>0) {
//            该标签已经发给哪些车辆
            StringBuilder trucks = new StringBuilder();
            for (CTrucks c : one) {
                trucks.append(c.getTrucksTruckno()).append("、");
            }
            return SzResult.error("该标签已经发给" + trucks + "车辆了,请勿重复发放");
        }
        if (value == 1) {
            //判断标签1是否存在，存在则报错，不存在则发放
            if ((cTrucks.getTrucksTag1() != null && !cTrucks.getTrucksTag1().equals(""))) {
                return SzResult.Error("该车辆已有该标签,请勿重复发放");
            } else {
                cTrucks.setTrucksTag1(tag);
            }
        }
        if (value == 2) {
            //判断标签2是否存在，存在则报错，不存在则发放
            if (cTrucks.getTrucksTag2() != null && !cTrucks.getTrucksTag2().equals("")) {
                return SzResult.Error("该车辆已有该标签,请勿重复发放");
            } else {
                cTrucks.setTrucksTag2(tag);
            }
        }
        if (value == 3) {
            //判断标签3是否存在，存在则报错，不存在则发放
            if (cTrucks.getTrucksTag3() != null && !cTrucks.getTrucksTag3().equals("")) {
                return SzResult.Error("该车辆已有该标签,请勿重复发放");
            } else {
                cTrucks.setTrucksTag3(tag);
            }
        }
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        cTrucks.setTrucksFixMan(user.getTrueName());
        cTrucks.setDownedID1(0);
        icTrucksService.updateById(cTrucks);
        String trucksTruckno = cTrucks.getTrucksTruckno();
        sysLogBu.logRecord("发放标签", user.getTrueName(), "发放了车牌号为" + trucksTruckno + "的标签"+tag, plantId);
        return SzResult.success("标签发放成功");
    }

    //删除标签
    public SzResult deleteTag(String id, Integer value, String token,String plantId) throws Exception {
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        CTrucks cTrucks = icTrucksService.getOne(queryWrapper);
        String tag = "";
        if (value == 1) {
            //判断标签1是否存在，存在则删除，不存在则报错
            if (cTrucks.getTrucksTag1() != null && !cTrucks.getTrucksTag1().equals("")) {
                tag = cTrucks.getTrucksTag1();
                cTrucks.setTrucksTag1("");
            } else {
                return SzResult.Error("标签1不存在");
            }
        }
        if (value == 2) {
            //判断标签2是否存在，存在则删除，不存在则报错
            if (cTrucks.getTrucksTag2() != null && !cTrucks.getTrucksTag2().equals("")) {
                tag = cTrucks.getTrucksTag2();
                cTrucks.setTrucksTag2("");
            } else {
                return SzResult.Error("标签2不存在");
            }
        }
        if (value == 3) {
            //判断标签3是否存在，存在则删除，不存在则报错
            if (cTrucks.getTrucksTag3() != null && !cTrucks.getTrucksTag3().equals("")) {
                tag = cTrucks.getTrucksTag3();
                cTrucks.setTrucksTag3("");
            } else {
                return SzResult.Error("标签3不存在");
            }
        }
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        cTrucks.setTrucksFixMan(user.getTrueName());
        cTrucks.setTrucksFixTm(LocalDateTime.now());
        cTrucks.setDownedID1(0);
        icTrucksService.updateById(cTrucks);
        String trucksTruckno = cTrucks.getTrucksTruckno();
        sysLogBu.logRecord("删除标签", user.getTrueName(), "删除了车牌号为" + trucksTruckno + "的标签"+tag, plantId);
        return SzResult.success("标签删除成功");
    }
}
